package com.frkj.yuan.adapter;

import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.frkj.yuan.R;
import com.frkj.yuan.activity.OrderCommentActivity;
import com.frkj.yuan.entity.MultipleChoose;
import com.frkj.yuan.listener.OnItemClickListener;
import com.frkj.yuan.view.CheckableLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/5/18 15 ：13
 * 项目名称：yuanban
 */
public class OrderCommentAdapter extends RecyclerView.Adapter<OrderCommentAdapter.viewHolder> {
    private OrderCommentActivity activity;
    private List<MultipleChoose> mlist = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
    public OrderCommentAdapter(OrderCommentActivity activity, List<MultipleChoose> mlist) {
        this.activity = activity;
        this.mlist = mlist;
    }
    public void update(List<MultipleChoose> list){
        if(list != null && list.size() > 0){
            mlist.addAll(list);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public OrderCommentAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View itemView = LayoutInflater.from(activity).inflate(R.layout.recyclerview_item, viewGroup, false);
        return new viewHolder(itemView);
    }
    @Override
    public void onBindViewHolder(@NonNull OrderCommentAdapter.viewHolder holder, int position) {
        Set<Integer> positionSet = OrderCommentActivity.positionSet;
        if (positionSet.contains(position)) {
            holder.check_able.setChecked(true);
            holder.textView.setTextColor(Color.parseColor("#ffffff"));
            holder.textView.setBackgroundResource(R.drawable.circle_border_origin_bg);
        } else {
            holder.check_able.setChecked(false);
            holder.textView.setTextColor(Color.parseColor("#333333"));
            holder.textView.setBackgroundResource(R.drawable.border_circle_grew_bg);
        }
        if(mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemClick(holder.itemView, pos);
                    holder.check_able.setChecked(true);
                }
            });
        }
        holder.textView.setText(mlist.get(position).getName());
    }
    @Override
    public int getItemCount() {
        return mlist == null ? 0 : mlist.size();
    }
    public class viewHolder extends RecyclerView.ViewHolder  {
        TextView textView;
        CheckableLayout check_able;
        public viewHolder(@NonNull View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.item_name);
            check_able = (CheckableLayout)itemView.findViewById(R.id.check_able);
        }
    }
}
